<template>
  <div class="app-container">

    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input placeholder="请输入用户名..." v-model="queryForm.query" clearable></el-input>
      </el-col>
      <el-button type="primary" :icon="Search" @click="initActList">搜索</el-button>

      <el-popconfirm title="您确定批量删除这些记录吗？" @confirm="handleDelete(null)">
        <template #reference>
          <el-button type="danger" :disabled="delBtnStatus" :icon="Delete"
            v-if="hasAuth('system:user:delete')">批量删除</el-button>
        </template>
      </el-popconfirm>
    </el-row>
    <el-table :data="tableData" stripe style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="imgUrl" label="插图" width="80" align="center">
        <template v-slot="scope">
          <el-image :src="listForImg(scope.row.imgUrl)[0]" fit="cover" :zoom-rate="1.2" preview-teleported="true"
            :preview-src-list="listForImg(scope.row.imgUrl)" width="50" height="50" />
        </template>
      </el-table-column>
      <el-table-column prop="actNumber" label="活动编号" width="110" align="center" />
      <el-table-column prop="actName" label="活动名称" width="100" align="center" />
      <el-table-column prop="clubNumber" label="所属社团编号" width="120" align="center" />
      <el-table-column prop="clubName" label="所属社团名称" width="120" align="center" />
      <el-table-column prop="actAddress" label="活动场地" width="100" align="center" />
      <el-table-column prop="actMessage" label="活动内容" show-overflow-tooltip="true" width="100" align="center" />
      <el-table-column prop="actStatus" label="审核状态" :formatter="converStatus" width="100" align="center" />
      <el-table-column prop="createTime" label="创建时间" width="160" align="center" />
      <el-table-column prop="action" label="操作" width="400" fixed="right" align="center">
        <template v-slot="scope">
          <el-button @click="showActMsg(scope.row.actId)" v-if="hasAuth('system:user:role')">查看详情</el-button>

          <el-button v-if="hasAuth('system:user:edit')" type="primary" :icon="Edit"
            @click="audit(scope.row.actId, '1')">通过</el-button>

          <el-button v-if="hasAuth('system:user:edit')" type="danger" :icon="Edit"
            @click="audit(scope.row.actId, '2')">不通过</el-button>

          <el-popconfirm v-if="scope.row.username != 'java1234'" title="您确定要删除这条记录吗？"
            @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button type="danger" :icon="Delete" v-if="hasAuth('system:user:delete')" />
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination v-model:currentPage="queryForm.pageNum" v-model:page-size="queryForm.pageSize"
      :page-sizes="[10, 20, 30, 40, 50]" layout="total, sizes, prev, pager, next, jumper" :total="total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
  <Dialog v-model="dialogVisible" :dialogVisible="dialogVisible" :id="id" :dialogTitle="dialogTitle"
    @initActList="initActList"></Dialog>
</template>

<script setup>
import { Search, Delete, Edit, Tools } from '@element-plus/icons-vue'
import { ref } from 'vue'
import requestUtil, { getServerUrl } from "@/utils/request";
import { ElMessage } from 'element-plus'
import Dialog from './components/dialog'
import ClubStatusEnum from '@/enum/clubStatusEnum'

const queryForm = ref({
  query: '',
  pageNum: 1,
  pageSize: 10
})

const total = ref(0)

const tableData = ref([])

const id = ref(-1)

const dialogVisible = ref(false)

const dialogTitle = ref('')

const multipleSelection = ref([])

const delBtnStatus = ref(true)

const handleSelectionChange = (selection) => {
  console.log("勾选了")
  console.log(selection)
  multipleSelection.value = selection;
  delBtnStatus.value = selection.length == 0;
}


const initActList = async () => {
  const res = await requestUtil.post("/activity/listTActivity", queryForm.value)
  tableData.value = res.data.actList;
  total.value = res.data.total;
}

/**
 * 状态枚举转换
 * @param {行} row 
 * @param {列} column 
 * @param {该行该列的值} cellValue 
 */
const converStatus = (row, column, cellValue) => {
  return ClubStatusEnum.getDesc(cellValue);
}

initActList();

/**
 * 对图片列表进行前缀拼接处理
 * @param {不带前缀的图片路径} imageUrls 
 */
const listForImg = (imageUrls) => {
  console.log(imageUrls);
  let newList = [];
  if (imageUrls) {
    imageUrls.forEach(item => {
      newList.push(getServerUrl() + item);
    })
  }
  return newList;
}

const handleSizeChange = (pageSize) => {
  queryForm.value.pageNum = 1;
  queryForm.value.pageSize = pageSize;
  initActList();
}

const handleCurrentChange = (pageNum) => {
  queryForm.value.pageNum = pageNum;
  initActList();
}

const handleDelete = async (id) => {
  var ids = []
  if (id) {
    ids.push(id)
  } else {
    multipleSelection.value.forEach(row => {
      ids.push(row.id)
    })
  }
  const res = await requestUtil.post("sys/user/delete", ids)
  if (res.data.code == 200) {
    ElMessage({
      type: 'success',
      message: '执行成功!'
    })
    initActList();
  } else {
    ElMessage({
      type: 'error',
      message: res.data.msg,
    })
  }
}

const showActMsg = (actId) => {
  dialogVisible.value = true;
  id.value = actId;
}

const audit = (actId, type) => {

}


</script>

<style lang="scss" scoped>
.header {
  padding-bottom: 16px;
  box-sizing: border-box;
}

.el-pagination {
  float: right;
  padding: 20px;
  box-sizing: border-box;
}

::v-deep th.el-table__cell {
  word-break: break-word;
  background-color: #f8f8f9 !important;
  color: #515a6e;
  height: 40px;
  font-size: 13px;

}

.el-tag--small {
  margin-left: 5px;
}
</style>
